package com.jeesite.modules.dataScreen.web;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.constants.SaTokenConstants;
import com.jeesite.modules.dataScreen.constants.ValidationGroup;
import com.jeesite.modules.dataScreen.entity.SysMenu;
import com.jeesite.modules.dataScreen.entity.SysRole;
import com.jeesite.modules.dataScreen.service.SysMenuService;
import com.jeesite.modules.dataScreen.service.SysRoleService;
import com.jeesite.modules.dataScreen.service.SysUserService;
import com.jeesite.modules.dataScreen.web.dto.*;
import com.jeesite.modules.dataScreen.web.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.List;

@RequestMapping("/management")
@RestController
@Api(tags = "后台管理Api")
@SaCheckRole(SaTokenConstants.ADMIN_ROLE)
public class BgManagementApi {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @SaIgnore
    public Result<LoginVo> login(@RequestBody @Validated LoginDto dto) {
        return Result.OK(sysUserService.login(dto));
    }

    @GetMapping("/user/listPage")
    @ApiOperation("用户-列表")
    public Result<List<SysUserPageVo>> listUser(SysUserPageSearchDto dto) {
        return Result.OK(sysUserService.list(dto));
    }

    @GetMapping("/user/detail/{id}")
    @ApiOperation("用户-详情")
    public Result<SysUserVo> detailUser(@PathVariable Integer id) {
        return Result.OK(sysUserService.detail(id));
    }

    @PostMapping("/user/create")
    @ApiOperation("用户-新增")
    public Result<String> createUser(@RequestBody @Validated({ValidationGroup.CREATE.class, Default.class}) SysUserDto dto) {
        sysUserService.update(dto);
        return Result.OK();
    }

    @PostMapping("/user/update")
    @ApiOperation("用户-修改")
    public Result<String> updateUser(@RequestBody @Validated({ValidationGroup.UPDATE.class, Default.class}) SysUserDto dto) {
        sysUserService.update(dto);
        return Result.OK();
    }

    @GetMapping("/user/freeze")
    @ApiOperation("用户-禁用")
    public Result<String> freezeUser(@RequestParam Integer userId) {
        sysUserService.freeze(userId);
        return Result.OK();
    }

    @GetMapping("/user/undoFreeze")
    @ApiOperation("用户-解禁")
    public Result<String> undoFreezeUser(@RequestParam Integer userId) {
        sysUserService.undoFreeze(userId);
        return Result.OK();
    }

    @PostMapping("/user/resetPass")
    @ApiOperation("用户-重置密码")
    public Result<String> resetPass(@RequestBody JSONObject dto) {
        sysUserService.resetPassword(dto);
        return Result.OK();
    }

    @GetMapping("/user/delete")
    @ApiOperation("用户-删除")
    public Result<String> deleteUser(@RequestParam Integer userId) {
        sysUserService.delete(userId);
        return Result.OK();
    }

    @GetMapping("/role/listPage")
    @ApiOperation("角色-列表")
    public Result<List<SysRole>> listPageRole(RoleSearchDto searchDto) {
        return Result.OK(sysRoleService.listPage(searchDto));
    }

    @GetMapping("/role/detail/{id}")
    @ApiOperation("角色-详情")
    public Result<SysRoleVo> detailRole(@PathVariable Integer id) {
        return Result.OK(sysRoleService.findById(id));
    }

    @PostMapping("/role/create")
    @ApiOperation("角色-新增")
    public Result<String> createRole(@RequestBody
                                     @Validated({ValidationGroup.CREATE.class, Default.class}) SysRoleDto dto) {
        sysRoleService.update(dto);
        return Result.OK();
    }

    @PostMapping("/role/update")
    @ApiOperation("角色-修改")
    public Result<String> updateRole(@RequestBody
                                     @Validated({ValidationGroup.UPDATE.class, Default.class}) SysRoleDto dto) {
        sysRoleService.update(dto);
        return Result.OK();
    }

    @GetMapping("/role/delete")
    @ApiOperation("角色-删除")
    public Result<String> deleteRole(@RequestParam Integer id) {
        sysRoleService.delete(id);
        return Result.OK();
    }

    @GetMapping("/menu/list")
    @ApiOperation("菜单-列表")
    public Result<List<SysMenu>> listMenus(@RequestParam(required = false) String name) {
        return Result.OK(sysMenuService.findAll(name));
    }

    @GetMapping("/menu/detail/{id}")
    @ApiOperation("菜单-详情")
    public Result<SysMenu> detailMenu(@PathVariable Integer id) {
        return Result.OK(sysMenuService.findById(id));
    }

    @PostMapping("/menu/create")
    @ApiOperation("菜单-新增")
    public Result<String> menuCreate(@RequestBody @Validated({ValidationGroup.CREATE.class, Default.class}) SysMenuDto dto) {
        sysMenuService.updateOrCreate(dto);
        return Result.OK();
    }

    @PostMapping("/menu/update")
    @ApiOperation("菜单-修改")
    public Result<String> menuUpdate(@RequestBody @Validated({ValidationGroup.UPDATE.class, Default.class}) SysMenuDto dto) {
        sysMenuService.updateOrCreate(dto);
        return Result.OK();
    }

    @GetMapping("/menu/delete")
    @ApiOperation("菜单-删除")
    public Result<String> menuDelete(@RequestParam(required = false) Integer id) {
        sysMenuService.delete(id);
        return Result.OK();
    }
}
